<template>
  <div class="container">
    <div class="dialog">
      <div class="title">业务系统账号管理</div>
      <div class="tab">
        <div>
          <el-tabs v-model="activeTab" @tab-click="switchTab">
            <el-tab-pane
              v-for="i in tabs"
              :key="i.value"
              :label="i.label"
            ></el-tab-pane>
          </el-tabs>
        </div>
        <div style="margin-left: auto">
          <el-button plain class="add" icon="el-icon-plus" @click="edit('add')"
            >添加账号</el-button
          >
          <el-button
            plain
            class="back"
            icon="el-icon-refresh-left"
            @click="back"
            >返回首页</el-button
          >
        </div>
      </div>
      <div>
        <el-table
          v-loading="loading"
          :data="tableData"
          height="400"
          border
          style="width: 100%"
        >
          <el-table-column prop="index" label="序号" width="80">
          </el-table-column>
          <el-table-column prop="website" label="系统" width="100">
            <template slot-scope="scope">
              <el-tag
                :color="scope.row.website === 'ECP' ? '#249087' : 'blue'"
                disable-transitions
                effect="dark"
              >
                {{ scope.row.website }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column prop="userName" label="用户名" width="220">
          </el-table-column>
          <el-table-column prop="password" label="密码" width="220">
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="120">
            <template slot-scope="scope">
              <el-button
                style="color: #249087"
                type="text"
                size="medium"
                @click="edit('edit', scope.row)"
                >编辑</el-button
              >
              <el-button
                type="text"
                size="medium"
                style="color: red"
                @click="deleteItem(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>

      <AccountEdit ref="editDialog" @freshData="freshData"></AccountEdit>
    </div>
  </div>
</template>

<script>
import AccountEdit from "./accountEdit.vue";
import { getEcp, getErp, deleteEcp, deleteErp } from "@/common/api";
export default {
  name: "AccontMange",
  props: {},
  components: {
    AccountEdit,
  },
  data() {
    return {
      loading: false,
      activeTab: "0",
      tabs: [
        {
          label: "ECP账号",
          value: "0",
        },
        {
          label: "ERP账号",
          value: "1",
        },
      ],
      tableData: [
        // {
        //   index: 1,
        //   website: "ECP",
        //   userName: "WANGCHAO",
        //   password: "*********",
        //   id: "1",
        // },
        // {
        //   index: 2,
        //   website: "ERP",
        //   userName: "JIEDONGWEI",
        //   password: "*********",
        // },
        // {
        //   index: 3,
        //   website: "ECP",
        //   userName: "LICUNLONG",
        //   password: "*********",
        // },
        // {
        //   index: 4,
        //   website: "ERP",
        //   userName: "ALONG",
        //   password: "*********",
        // },
        // {
        //   index: 5,
        //   website: "ECP",
        //   userName: "YIZHI",
        //   password: "*********",
        // },
      ],
    };
  },
  created() {
    this.getEcpData();
  },
  methods: {
    getEcpData() {
      this.loading = true;
      getEcp()
        .then((res) => {
          this.loading = false;
          if (res.success == 1) {
            this.tableData = res.data.rows;
            this.tableData.forEach((i, index) => {
              i.index = index + 1;
            });
          } else {
            this.$errorMsg(res.message);
          }
        })
        .catch((err) => {
          this.loading = false;
          this.$errorMsg("接口异常");
        });
    },
    getErpData() {
      this.loading = true;
      getErp()
        .then((res) => {
          this.loading = false;
          if (res.success == 1) {
            this.tableData = res.data.rows;
            this.tableData.forEach((i, index) => {
              i.index = index + 1;
            });
          } else {
            this.$errorMsg(res.message);
          }
        })
        .catch((err) => {
          this.loading = false;
          this.$errorMsg("接口异常");
        });
    },

    switchTab(e) {
      if (e.index == 0) {
        this.getEcpData();
      } else if (e.index == 1) {
        this.getErpData();
      }
    },
    back() {
      this.$emit("back");
    },
    edit(e, item) {
      // console.log(item, "000");
      if (item) {
        //编辑
        const { userName, password, website, id } = item;
        this.$refs.editDialog.form = {
          userName,
          password,
          website,
          id,
        };
        this.$refs.editDialog.title = "编辑";
      } else {
        //新增
        this.$refs.editDialog.title = "新增";
        this.$refs.editDialog;
        this.$refs.editDialog.form = {
          userName: "",
          password: "",
          website: "ECP",
        };
      }
      this.$refs.editDialog.dialogFormVisible = true;
    },

    deleteItem(item) {
      // console.log(item, "111");
      this.$confirm("确认删除此账号?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delData(item);
        })
        .catch(() => {});
    },

    delData(e) {
      if (e.website == "ECP") {
        deleteEcp({ id: item.id })
          .then((res) => {
            if (res.success == 1) {
              this.$successMsg("删除成功");
              this.getEcpData();
            } else {
              this.$errorMsg(res.message);
            }
          })
          .catch((err) => {
            this.$errorMsg(接口异常);
          });
      } else {
        deleteErp({ id: item.id })
          .then((res) => {
            if (res.success == 1) {
              this.$successMsg("删除成功");
              this.getErpData();
            } else {
              this.$errorMsg(res.message);
            }
          })
          .catch((err) => {
            this.$errorMsg(接口异常);
          });
      }
    },

    freshData() {
      if (this.activeTab == "0") {
        this.getEcpData();
      } else {
        this.getErpData();
      }
    },
  },
};
</script>


<style lang="scss" scoped >
.container {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  .dialog {
    background-color: white;
    width: 50%;
    height: 500px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2),
      /* 水平偏移 | 垂直偏移 | 模糊半径 | 阴影颜色与透明度 */ 0 6px 20px
        rgba(0, 0, 0, 0.19); /* 可以添加多个阴影效果 */
    border-radius: 14px;
    padding: 16px 20px 20px 20px;
    .title {
      text-align: left;
      color: #249087;
      font-weight: bold;
      font-size: 20px;
    }
    .tab {
      margin-top: 10px;
      display: flex;
      .add {
        background-color: #249087;
        color: white;
      }
      .back {
        color: #249087;
        border-color: #249087;
      }
    }
  }
}

::v-deep .el-tabs__item.is-active {
  color: #249087;
}
::v-deep .el-tabs__item:hover {
  color: #249087;
  cursor: pointer;
}
::v-deep .el-tabs__active-bar {
  background-color: #249087;
}
</style>